home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char SccsId[]= "@(#)pixmaps.c V1.16 3/13/95";
- #endif
- /*
- | file name - pixmaps.c
- |===================================================================
- |
- | This program demonstrates the use of pixmaps, images and icons.
- | It loads an existing view and adds an image and icon to the view;
- | the icon is also given a simple mask.
- |
- | The image acts as a large background canvas, and the icon is drawn
- | in front of it. The view contains an object named "dynamic.obj"
- | whose dynamics are attached to the icon.
- |
- | The images used in this example program are found in the
- | <dataviews>/lib/images directory.
- |
- | The programs ends if the user selects the <q|Q> key or the
- | right mouse button.
- |
- |===================================================================
- */
-
- /*
- * DV-Tools header files
- */
- #include "std.h" /* <stdio.h> etc., scalar & macro definitions */
- #include "dvstd.h" /* public types & constants */
- #include "dvtools.h" /* constants used by T routines */
- #include "dvGR.h" /* constants used by window mgt & GR routines */
- #include "VOstd.h" /* constants used by VO & VOob routines */
- #include "Tfundecl.h" /* T routines (screens, drawports & views) */
- #include "VOfundecl.h" /* VO routines (objects) */
- #include "VUerfundecl.h" /* VUer routines (event handling routines) */
- #include "VUfundecl.h" /* VU routines (utilities) */
-
- /* Constants */
- #define DVPATH (char *)NULL
- #define DISPFORMS_STB (char *)NULL
- #define DVDEVICE (char *)NULL
- #define DVCOLORTABLE (char *)NULL
- #define VIEW_NAME "pixmaps.v"
- #define SCREEN_VIEWPORT (RECTANGLE *)NULL
-
- /* Whole world rectangle, (-16384, -16384) to (16383, 16383)*/
- RECTANGLE whole_world = {XMIN, YMIN, XMAX, YMAX};
-
- /* Functions defined in pixmaps.c */
-
- /*
- * MAIN PROGRAM
- */
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow )
- {
- /*
- * program arguments
- * argv[1] - display device name (default is to use DVDEVICE)
- */
-
- /* Define & initialize device name and view filename */
- char *device_name = DVDEVICE; /* default device name */
- char *view_name = VIEW_NAME; /* default view name */
-
- /* Define display variables */
- OBJECT screen; /* display device, the window */
- DRAWPORT drawport; /* how & where to display picture, picture frame */
- VIEW view; /* picture representation of the view file */
-
- /* Control loop variables */
- OBJECT location; /* the event representation */
- int event_req_status; /* status of event requests */
-
- int argc = 0;
- char **argv;
- /* pixmap and image object variables */
- OBJECT drawing, /* graphical representation of screen */
- p1, p2, /* control points used by objects */
- pixmap, /* pixmap object */
- back_image, /* image object */
- fore_icon, /* icon object */
- dyn_obj, /* dynamic control object */
- dynamics; /* object with dynamics from main view */
- ATTRIBUTES attr; /* attributes structure */
- COLOR_TABLE *colorp; /* color table representing colors used by pixmap */
- COLOR_XFORM color_xform; /* color xform structure */
-
- /* Other variables */
- int Quit = NO, /* flag to quit program */
- index; /* counter */
-
- /*--------------------
- * Initialization
- *
- * TInit: perform the initialization of DV-Tools
- * TInit reads your configuration file and any
- * environment variables or logical names set.
- */
- make_argv(&argc,&argv,GetCommandLine());
- TInit( DVPATH, DISPFORMS_STB );
-
- /*
- * TscOpenSet: open a device as a screen object using
- * specified attributes
- *
- * Set exposure block to YES to insure the window
- * is ready for drawing when TdpDraw is called.
- */
- if (argc > 1)
- device_name = argv[1];
- screen = TscOpenSet (device_name, DVCOLORTABLE,
- V_X_EXPOSURE_BLOCK, YES,
- V_ACTIVE_CURSOR, V_END_OF_LIST);
- if (!screen)
- {
- printf ("Must specify device on command line or");
- printf (" in DataViews configuration file.\n");
- S_EXIT (EXIT_ERR);
- }
-
- /*
- * VOscWinEventMask: sets the screen's window event mask
- */
- VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS |
- V_EXPOSE | V_RESIZE | V_MOTIONNOTIFY,
- (ULONG) 0);
-
- /*
- * TviLoad: Load a view in from a file
- * TviGetDrawing: Gets a view's drawing object
- */
- view = TviLoad (view_name);
- if (!view)
- {
- printf ("Could not load view from file ");
- printf ("%s.\n", view_name);
- S_EXIT (EXIT_ERR);
- }
- drawing = TviGetDrawing (view);
-
- /*
- * TdpCreateStretch: Create a drawport
- * "drawport" is attached to the screen object, "screen"
- * "view" is the view to be displayed on the screen
- * SCREEN_VIEWPORT specifies the screen viewport
- * "whole_world" specifies the portion of the view to be
- * displayed, here the whole view
- * The whole view will be stretched to fit in the drawport
- */
- drawport = TdpCreateStretch (screen, view,
- SCREEN_VIEWPORT, &whole_world);
-
- /*
- * VOpmCreate: Create and returns a pixmap
- */
- pixmap = VOpmCreate ("monalisa.gif", (ADDRESS) NULL);
-
- /*
- * VOuAtInit: Sets all attribute fields to EMPTY_FIELD.
- * VOptCreate: Create a point object
- *
- * Initialize the attribute text position. This attribute
- * determines which point in the raster remains stationary
- * when the image is scaled. Create a image object from
- * the pixmap specifying the control points identifying
- * it's location.
- */
- VOuAtInit (&attr);
- attr.text_position = AT_BOTTOM_EDGE;
- p1 = VOptCreate (WORLD_COORDINATES, -15000, 11000, (OBJECT) NULL);
- p2 = VOptCreate (WORLD_COORDINATES, 15000, -8000, (OBJECT) NULL);
- back_image = VOimCreate (pixmap, p1, p2, &attr, V_IM_ATTR_ARGEND);
-
- /*
- * VOdrObAdd: Adds an object to the drawing
- */
- VOdrObAdd (drawing, back_image);
-
- /*
- * VOimScalePixmap: Displays an image at an exact scale factor
- *
- * Now adjust background so it is scaled exactly one pixmap pixel
- * per screen pixel. Thus it will look right at the current zoom factor,
- * but, being an image, will still change size if the drawport is zoomed.
- * Since the image's text position attribute is AT_BOTTOM_EDGE, if the
- * image expands it will do so along the top and side edges. The lower
- * edge stays fixed, and so can't overwrite the message or slider
- * that are under the image.
- */
- VOimScalePixmap (back_image, TdpGetXform (drawport, DR_TO_SCREEN),
- 1.0, 1.0);
-
- /*
- * VOpmCreate: Create and returns a pixmap
- * VOicCreate: Create an icon from a pixmap
- *
- * Create an icon object from the pixmap. An icon object displays a
- * raster which stays a fixed size on the screen even if the drawport
- * containing the object is zoomed. Therefore, the icon object only
- * specifies an single anchor point.
- */
- pixmap = VOpmCreate ("rose.gif", (ADDRESS) NULL);
- fore_icon = VOicCreate (pixmap, p1, (ATTRIBUTES *) NULL, V_IC_ATTR_ARGEND);
-
- /*
- * We now create a simple mask for this icon.
- * This will be done by using the icon's pixmap as the mask and using
- * a color transform to map all the colors in this pixmap to 1 except
- * a few colors. This works OK for "rose.gif" as most of the
- * background pixels use one of a few dark greys, and these greys are
- * used only as background. For other icons, more complex mask pixmaps
- * and/or color transforms might be more appropriate.
- */
-
- /*
- * VOpmGet: Get information about a pixmap
- *
- * Get the colors used by the pixmap.
- */
- VOpmGet (pixmap, V_PM_COLOR_TABLE, &colorp, V_PM_ATTR_ARGEND);
-
- /*
- * Loop through all the colors in the color table obtained from
- * the pixmap and set all the indices in the color xform structure
- * to 1. This makes all colors visible. This color xform structure
- * will be used to set the mapping of the pixmap's color indicies
- * to the screen's color indices.
- */
- color_xform.size = colorp->ctsize;
- for (index = 0; index < colorp->ctsize; index++)
- color_xform.new_index[index] = 1;
-
- /*
- * VUctRGBtoIndex: Finds the closest match to a color in a color table.
- *
- * Find the index of the background color and make it transparent.
- * For "rose.gif" the colors (16,16,16), (16,8,16), (16,16,8), (16,8,8)
- * are used for most of the background pixels. Set the index in the
- * color xform structure to 0 representing invisible.
- */
- VUctRGBtoIndex (colorp, 16, 16, 16, &index);
- color_xform.new_index[index] = 0;
- VUctRGBtoIndex (colorp, 16, 16, 8, &index);
- color_xform.new_index[index] = 0;
- VUctRGBtoIndex (colorp, 16, 8, 16, &index);
- color_xform.new_index[index] = 0;
- VUctRGBtoIndex (colorp, 16, 8, 8, &index);
- color_xform.new_index[index] = 0;
-
- /*
- * VOicSet: Set characteristics for an icon
- *
- * V_IC_MASK_PIXMAP: pixmap used as the writemask
- * V_IC_MASK_PIXMAP_XFORM: Mapping of the pixmap's color indices
- */
- // VOicSet (fore_icon, V_IC_MASK_PIXMAP, pixmap,
- // V_IC_MASK_PIXMAP_XFORM, &color_xform, V_IC_ATTR_ARGEND);
-
- /*
- * VOobDyGet: Returns the dynamic control object
- * attached to the object
- * VOobDySet: Associates a dynamic control bject with
- * a graphical object
- * VOdrObReplace: Replaces current object with new object
- * VOdrObTop: Moves an object to the top of the drawing
- *
- * Obtain the named object, "dynamic.obj" from the drawing. Get
- * this object's dynamic control object. Then associate this
- * dynamic control object with the created icon object. Replace
- * the icon object with the object from the drawing and move
- * the icon object to the top of the drawing list.
- */
- dyn_obj = TdrGetNamedObject (drawing, "dynamic.obj");
- dynamics = VOobDyGet (dyn_obj);
- VOobDySet (fore_icon, dynamics);
- VOdrObReplace (drawing, dyn_obj, fore_icon);
- VOdrObTop (drawing, fore_icon);
-
- /*
- * TscErase: Erase the entire screen in the default
- * background color
- * TdpDraw: Draw the contents of the drawport
- */
- TscErase (screen);
- TdpDraw (drawport);
-
- /*--------------------
- * Control loop
- *
- * Poll the event queue for window events specified by the
- * window mask. Handle each of the events as they happen.
- * Events occurring within input objects will be handled
- * through the event request handler VUerHandleLocEvent.
- * Updates are performed if an input object used the
- * the event.
- */
- FOREVER
- {
- /*
- * VOloWinEventPoll: Polls for the next window event.
- * The polling mode used is V_WAIT.
- * Therefore, VOloWinEventPoll does not
- * return until a masked event is
- * generated. V_WAIT always produces
- * a valid location object.
- */
- location = VOloWinEventPoll (V_WAIT);
-
- /*
- * VOloType: returns the type of event. These types
- * match event types specified in VOscWinEventMask.
- */
- switch (VOloType (location))
- {
-
- case V_EXPOSE:
- /*
- * VOloRegion: Returns a rectangle representing the
- * exposed region on the screen.
- * TscRedraw: After erasing, redraws all the drawports
- * in the screen.
- * A portion of the window has been exposed and needs
- * to be redrawn.
- */
- TscRedraw (screen, VOloRegion (location));
- break;
-
- case V_RESIZE:
- /*
- * The window size has been changed.
- * TscReset: Resets all screen drawports after
- * window resizing
- */
- TscReset (screen);
- break;
-
- case V_KEYPRESS:
- /*
- * Check key selected.
- * VOloKeySym: Returns the key symbol value of the
- * location object
- * If the key symbol represents the characters 'q'
- * or 'Q' then quit the program.
- */
- switch (VOloKeySym (location))
- {
- case 'q':
- case 'Q':
- Quit = YES;
- break;
-
- default:
- break;
- }
- break;
-
- case V_BUTTONPRESS:
- case V_MOTIONNOTIFY:
- /*
- * VUerHandleLocEvent: Service the event.
- * This routine will check if the
- * event is used by any input objects
- * that are in the view.
- */
- event_req_status = VUerHandleLocEvent (location);
-
- /*
- * Update view only if an input object used the event
- * TdpDrawNext: Update all dynamic objects within a
- * drawport's view.
- */
- if (event_req_status != INPUT_UNUSED)
- TdpDrawNext (drawport);
- break;
-
- default:
- break;
- }
-
- /* exit the program */
- if (Quit == YES)
- break;
- }
-
- /*--------------------
- * Termination
- *
- * TscErase: Erase the entire screen in the default
- * background color
- * TdpDestroy: Destroy the drawport,
- * TviDestroy: Destroy the view, freeing the allocated memory
- * TscCloseCurrentScreen: Close the current display screen
- * TTerminate: Perform the clean-up for DV-Tools
- */
- TscErase (screen);
- TdpDestroy (drawport);
- TviDestroy (view);
- TscCloseCurrentScreen ();
- TTerminate ();
- return EXIT_OK;
- }
-